package xxl.robotlegs.extensions.signalcommandmap.impl 
{
	import flash.utils.getQualifiedClassName;
	import xxl.robotlegs.extensions.commandcenter.api.ICommandUnmapper;
	import xxl.robotlegs.extensions.commandcenter.api.ICommandMapper;
	import xxl.robotlegs.extensions.commandcenter.impl.CommandTriggerMap;
	import xxl.robotlegs.extensions.signalcommandmap.api.ISignalCommandMap;
	import xxl.robotlegs.framework.api.IContext;
	import xxl.robotlegs.framework.api.IInjector;
	import xxl.robotlegs.framework.api.ILogger;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class SignalCommandMap implements ISignalCommandMap 
	{
		private var _injector:IInjector;
		private var _logger:ILogger;
		private var _triggerMap:CommandTriggerMap;
		private const _mappingProcessors:Array = [];
		
		public function SignalCommandMap(context:IContext)
		{
			_injector = context.injector;
			_logger = context.getLogger(this);
			_triggerMap = new CommandTriggerMap(getKey, createTrigger);
		}
		
		//----------private
		
		private function getKey(signalClass:Class, injectName:String, mapKey:Object):String
		{
			return getQualifiedClassName(signalClass) + "|" + injectName + "_" + mapKey;
		}
		
		private function createTrigger(signalClass:Class, injectName:String, mapKey:Object):SignalCommandTrigger
		{
			return new SignalCommandTrigger(_injector, signalClass, injectName, mapKey, _mappingProcessors, _logger);
		}
		
		private function getTrigger(signalClass:Class, injectName:String, mapKey:Object):SignalCommandTrigger
		{
			return _triggerMap.getTrigger(signalClass, injectName, mapKey) as SignalCommandTrigger;
		}
		
		/* INTERFACE xxl.robotlegs.extensions.signalcommandmap.api.ISignalCommandMap */
		
		public function map(signalClass:Class, injectName:String = "", mapKey:Object = null):ICommandMapper 
		{
			return getTrigger(signalClass, injectName, mapKey || "").createMapper();
		}
		
		public function unmap(signalClass:Class, injectName:String = "", mapKey:Object = null):ICommandUnmapper 
		{
			return getTrigger(signalClass, injectName, mapKey || "").createMapper();
		}
		
		public function addMappingProcessor(handler:Function):ISignalCommandMap 
		{
			if (_mappingProcessors.indexOf(handler) < 0)
			{
				_mappingProcessors.push(handler);
			}
			return this;
		}
		
	}

}